<?php
	class infoListModel extends Model{

		/*
			数组转换成字符串
			@param   array()
			return string
		*/
		function toString($arr){
			$str="";
			if(!is_array($arr))return false;
			$str.="array(\r\n";
			foreach($arr as $k=>$v){
				$str.="'".$k."'=>'array(\r\n";
				if(is_array($v)){
					foreach($v as $key=>$value){
						$str.=" '".$key."'=>'".$value."',\r\n";
					}
					
				}
				$str.="),\r\n";
			$str.="),\r\n";
			}
			return $str;
		}
		//根据分类id获得所有子类ids
		function getLids($id=''){
			$result = $this->where('id='.$id)->one();
			$path = $result['path'];
			$ipath = $path."-".$id;

			$result = $this->where("id=$id or (path like '%".$ipath."%')")->all();
			
			if($result){
				$ids = '';
				foreach($result as $v){
					$ids .=$v['id'].',';
				}
				return trim($ids,',');
			}
		}
		//ajax修改isshow
		function ajaxShow($id,$zt){
			$result = $this->where('id='.$id)->one();
			if($result){//存在分类id信息，进行修改
				
				$sql = "update ica_infoList set isshow=$zt where id=$id or (path like '".$result['path'].'-'.$id."')";
				$this->query($sql);
			
				
					$sql ="select `id` from ica_infoList where id=$id or (path like '".$result['path'].'-'.$id."')";
					$ids = $this->query($sql);
					//$ids = $this->where("id=$id or (path like '".$result['path'].'-'.$id."')")->all();
					return $ids;
				
			}else{
				return false;
			}
		}
		//删除分类（同时删除此分类下分类）delList
		function delList($id){
			$result = $this->where('id='.$id)->one();
			$path = $result['path'];
			$ipath = $path."-".$id;
			$result = $this->where("id=$id or (path like '%".$ipath."%')")->del();
			return $result?true:false;
			//echo $this->getLastSql();
			
		}
		//获得分类下拉列表项字符串 传入第二个参数则此id为选中
		function getList($arr='',$id='',$sel=''){
			if(!$arr){//zt为真，则
				$sql = "select `id`,`name`,`path`,concat(`path`,'-',`id`) as ipath from ica_infoList where isshow=1 order by ipath";
				$arr = $this->query($sql);
			}

			$str='';
			if($id){//存在id
				foreach($arr as $v){
					if($v['id']==$id){
						$str.="<option value='".$v['id']."' selected='selected'>";
					}else{
						$str.="<option value='".$v['id']."'>";
					}
				
				$str.="|-";
				$num = count(explode('-',$v['path']));
				$str.=str_repeat('-',$num);
				$str.=$v['name'];
				$str.='</option>\r\n';
				}	
			
			}else{//没有传id
				if($sel){//返回带selectjs判断字符串
					foreach($arr as $v){
						if(isset($_GET['fenlei']) &&$_GET['fenlei']==$v['id']){
							$str.="<option value='".$v['id']."' selected='selected'>";
						}else{
							$str.="<option value='".$v['id']."'>";
						}
					
					$str.="|-";
					$num = count(explode('-',$v['path']));
					$str.=str_repeat('-',$num);
					$str.=$v['name'];
					$str.='</option>\r\n';
					}
				}else{
					foreach($arr as $v){
					$str.="<option value='".$v['id']."'>";
					$str.="|-";
					$num = count(explode('-',$v['path']));
					$str.=str_repeat('-',$num);
					$str.=$v['name'];
					$str.='</option>\r\n';
					}
				}
						
			}
			
			return $str;
		}

		//获得path
		function getIpath($id){
			$r = $this->field("path")->where("id=$id")->one();
			return $r['path']."-".$id;
		}
	}
?>